//
//  ZRCellOrginal.swift
//  Sina
//
//  Created by 张锐 on 16/3/1.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit
import SnapKit
class ZRCellOrginal: UIView {
    
    let kMargin = 10
    
    var constraint : Constraint?

    var statusViewModel : ZRHomeCellViewModel?{
    
        didSet{
            
            
            
            photoImageView.sd_setImageWithURL(statusViewModel?.photo_url)
            
            cornerImageView.image = statusViewModel?.verified_image
            
            vipImageView.image = statusViewModel?.mbrank_image
            
            nameLabel.text = statusViewModel?.statusModel?.user?.screen_name
            
            createTimeLabel.text = statusViewModel?.create_time
            
            sendSourceLabel.text = statusViewModel?.source_str
            
            contentTextLabel.text = statusViewModel?.statusModel?.text
            
            
            self.constraint?.uninstall()
            
            if let pic_count = statusViewModel?.statusModel?.pic_urls where pic_count.count > 0 {
                
                pictureCollectionView.hidden = false
                
                self.snp_updateConstraints { (make) -> Void in
                    
                    self.constraint = make.bottom.equalTo(pictureCollectionView.snp_bottom).constraint
                    
                }
                //传值
                pictureCollectionView.picViewModel = statusViewModel
            }else{
            
                pictureCollectionView.hidden = true

                self.snp_updateConstraints { (make) -> Void in
                    
                    self.constraint = make.bottom.equalTo(contentTextLabel.snp_bottom).constraint
                    
                    
                }
            
            }
            
        
        }
    
    }
    
    //重写构造方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //私有添加控件方法和设置约束
    
    private func setupUI(){
        
        
        addSubview(photoImageView)
        addSubview(cornerImageView)
        addSubview(nameLabel)
        addSubview(createTimeLabel)
        addSubview(sendSourceLabel)
        addSubview(vipImageView)
        addSubview(contentTextLabel)
        
        addSubview(pictureCollectionView)
        
 /// 建立约束
        
        photoImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top).offset(kMargin)
            
            make.left.equalTo(self.snp_left).offset(kMargin)
            
            make.size.equalTo(CGSizeMake(40, 40))
            
        }
        cornerImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(photoImageView.snp_right)
            
            make.centerY.equalTo(photoImageView.snp_bottom)
            
            
            
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top).offset(kMargin)
            
            make.left.equalTo(photoImageView.snp_right).offset(kMargin)
            
            
            
        }
        vipImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(nameLabel.snp_centerY)
            
            make.left.equalTo(nameLabel.snp_right).offset(kMargin)
            
            make.size.equalTo(CGSizeMake(15, 15))
            
            
            
        }
        createTimeLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(nameLabel.snp_bottom).offset(kMargin)
            
            make.left.equalTo(photoImageView.snp_right).offset(kMargin)
            
            
            
        }
        sendSourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(createTimeLabel.snp_centerY)
            
            make.left.equalTo(createTimeLabel.snp_right).offset(kMargin)
            
            
            
        }
        contentTextLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(photoImageView.snp_bottom).offset(kMargin)
            
            make.left.equalTo(self.snp_left).offset(kMargin)
            
//            make.width.equalTo(UIScreen.mainScreen().bounds.width - CGFloat (2 * kMargin))
            
            make.right.equalTo(self.snp_right).offset(-kMargin)
            
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
        pictureCollectionView.snp_makeConstraints { (make) -> Void in
            
        make.top.equalTo(contentTextLabel.snp_bottom).offset(kMargin)
            
            make.left.equalTo(self.snp_left).offset(kMargin)
            
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
          self.constraint = make.bottom.equalTo(pictureCollectionView.snp_bottom).constraint
            

        }
        
    
    
    }
    
    //懒加载控件
    /// 头像
    lazy var photoImageView :UIImageView = {
    
        let photoImageView = UIImageView()
        
        photoImageView.image = UIImage(named: "avatar_default_big")
        
        return photoImageView
    }()
    /// 头像角上的认证
    lazy var cornerImageView : UIImageView = {
    
        let cornerImageView = UIImageView()
        
        cornerImageView.image = UIImage(named: "avatar_vip")
        
        cornerImageView.sizeToFit()
        
        return cornerImageView
    
    }()
    /// 微博名
    lazy var nameLabel: UILabel = {
    
        let nameLabel = UILabel()
        
        nameLabel.text = "博主名字"
        
        nameLabel.font = UIFont.systemFontOfSize(14)
        
        return nameLabel
    
    }()
    /// vip图片
    lazy var vipImageView : UIImageView = {
    
        let vipImageView = UIImageView()
        
        vipImageView.image = UIImage(named: "common_icon_membership")
        
        return vipImageView
    
    }()
    /// 发表时间
    lazy var createTimeLabel :UILabel = {
    
        let createTimeLabel = UILabel()
        
        createTimeLabel.text = "微博发表时间"
        
        createTimeLabel.textColor = UIColor.orangeColor()
        
        createTimeLabel.font = UIFont.systemFontOfSize(13)
        
        return createTimeLabel
    
    }()
    /// 发表来源
    lazy var sendSourceLabel : UILabel = {
    
        let sendSourceLabel = UILabel()
        
        sendSourceLabel.text = "微博来源"
        
        sendSourceLabel.font = UIFont.systemFontOfSize(13)
        
        sendSourceLabel.textColor = UIColor.darkGrayColor()

        return sendSourceLabel
        
    }()
    /// 微博内容
    lazy var contentTextLabel :UILabel = {
    
        let contentTextLabel = UILabel()
        
        contentTextLabel.text = "微博内容"
        
        contentTextLabel.numberOfLines = 0
        
        contentTextLabel.font = UIFont.systemFontOfSize(14)

        return contentTextLabel
    
    }()
    
    private lazy var pictureCollectionView : ZRCellPic = ZRCellPic()
    

}
